import Config from './config.js'
import Util from './utils.js'
import store from '@/store/index.js'
import aes_utils from '@/js_sdk/aesutils/aes_utils.js'
import preutils from './preutils.js'
import check from './check.js'
import oauth from './oauth.js'
// #ifdef H5
const app_type = Util.isWeiXin() ? 'wechat' : 'h5';
const app_type_name = Util.isWeiXin() ? '微信公众号' : 'H5';
// #endif

// #ifdef MP-WEIXIN
const app_type = 'weapp';
const app_type_name = '微信小程序';
// #endif

// #ifdef MP-ALIPAY
const app_type = 'aliapp';
const app_type_name = '支付宝小程序';
// #endif

// #ifdef MP-BAIDU
const app_type = 'baiduapp';
const app_type_name = '百度小程序';
// #endif

// #ifdef MP-TOUTIAO
const app_type = 'MP-TOUTIAO';
const app_type_name = '头条小程序';
// #endif

// #ifdef MP-QQ
const app_type = 'MP-QQ';
const app_type_name = 'QQ小程序';
// #endif

// #ifdef APP-PLUS
// const app_type = 'app';
// const app_type_name = 'APP';
const app_type = 'weapp';
const app_type_name = '微信小程序';
// #endif

export default {	 
	sendRequest(params) {
		var url = ''
		var method = ''
		url = Config.baseUrl + params.api.url; // 请求路径
		method = params.api.method;
		if (method == undefined) {
			method = 'GET';
		}
		try {
			// var accessToken = uni.getStorageSync(preutils.accessToken);
			var loginToken = uni.getStorageSync(preutils.loginToken);
			// var userType = uni.getStorageSync(preutils.userType);
		} catch (e) {

		}

		if (!check.isNull(loginToken)) {
			params.data.token = loginToken;
		}

		let params_decrypt = {
			...params.data
		}
		if (params.api.isAes) {
			for (let key in params.data) { // 在这里判断传过来的参数值为null，就删除这个属性
				params.data[key] = aes_utils.AesEncrypt(params.data[key])
			}
		} 

		var headers = {
			// 'token': loginToken,
			 // 'Access-Control-Request-Method' : '*', // 接收的请求方法
			  // 'Access-Control-Request-Headers' : '*', // 请求头接收类型
			  // 'Access-Control-Allow-Credentials' : false,  // 是否允许请求携带 cookie，注意这里如果设置为true，则会报错 
			  // 'Access-Control-Max-Age' : 3600, // 缓存，可不设置
			'Content-Type': 'application/x-www-form-urlencoded;application/json;charset=utf-8;multipart/form-data;',
		}
		params.header = headers
 
		var data = {
			// app_type,
			// app_type_name
		};
		// 参数
		if (params.data != undefined) Object.assign(data, params.data);
		if (params.async === false) {
			//同步
			return new Promise((resolve, reject) => {
				uni.request({
					url: url,
					method: method,
					data: data,
					header: params.header,
					dataType: params.dataType || 'json',
					responseType: params.responseType || 'text',
					success: (res) => {
						// this.printSuccessLog(url,method,params_decrypt,params,'success',res)
						// if (res.data.code == -3 && store.state.siteState > 0) {
						// 	store.commit('setSiteState', -3)
						// 	Util.redirectTo('/otherpages/storeclose/storeclose/storeclose', {}, 'reLaunch');
						// 	return;
						// }
						// if (res.data.code == -4) {
						// 	store.commit('setSiteState', -4)
						// 	Util.redirectTo('/newaddpages/applyCheck/applyCheck', {}, 'reLaunch');
						// 	return;
						// }
						if (res.data.refreshtoken) {
							uni.setStorage({
								key: 'token',
								data: res.data.refreshtoken
							});
						}
						if (res.data.code == -10009 || res.data.code == -10010) {
							uni.removeStorage({
								key: 'token'
							})
						}
						if (params.api.isAes) {
							if (res.data.success == 'true') {
								var decryptdata = JSON.parse(aes_utils.AesDecrypt(res.data.data));
								res.data.data = decryptdata;
								resolve(res.data);
							} else {
								resolve(res.data);
							}
						} else {
							resolve(res.data);
						}
						this.printSuccessLog(url, method, params_decrypt, params, 'success', res)
					},
					fail: (res) => {
						this.printErrLog(url, method, params_decrypt, params, 'fail', res)
						reject(res);
					},
					complete: (res) => {
						this.printCompleteLog(url, method, params_decrypt, params, 'complete', res)
						reject(res);
					}
				});
			}).catch((e) => {});
		} else {
			//异步
			uni.request({
				url: url,
				method: method,
				data: data,
				header: params.header || {
					'content-type': 'application/x-www-form-urlencoded;application/json'
				},
				dataType: params.dataType || 'json',
				responseType: params.responseType || 'text',
				success: (res) => {
					console.log(res)
					// this.printSuccessLog(url,method,params_decrypt,params,'success',res)
					// if (res.data.code == -3 && store.state.siteState > 0) {
					// 	store.commit('setSiteState', -3)
					// 	Util.redirectTo('/otherpages/storeclose/storeclose/storeclose', {}, 'reLaunch');
					// 	return;
					// }
					//  if (res.data.code == -4 ) {
					//  	store.commit('setSiteState', -4)
					//  	Util.redirectTo('/newaddpages/applyCheck/applyCheck', {}, 'reLaunch');
					//  	return;
					//  }
					if (res.statusCode == 401) {
						Util.redirectToHome(true);
						return;
					}
					if (res.statusCode == 407) {
						// oauth.getOauthData();
					}
					
					if (res.data.refreshtoken) {
						uni.setStorage({
							key: 'token',
							data: res.data.refreshtoken
						});
					}
					if (res.data.code == -10009 || res.data.code == -10010) {
						uni.removeStorage({
							key: 'token'
						})
					}
					if (params.api.isAes) {
						if (res.data.success == 'true') {
							var decryptdata = JSON.parse(aes_utils.AesDecrypt(res.data.data));
							res.data.data = decryptdata;
						}
					}
					this.printSuccessLog(url, method, params_decrypt, params, 'success', res)
					typeof params.success == 'function' && params.success(res.data);
				},
				fail: (res) => {
					if (res.statusCode == 401) {
						Util.redirectToHome(true);
						return;
					}
					if (res.statusCode == 407) {
						// oauth.getOauthData();
					}

					this.printErrLog(url, method, params_decrypt, params, 'fail', res)
					typeof params.fail == 'function' && params.fail(res);
				},
				complete: (res) => {
					this.printCompleteLog(url, method, params_decrypt, params, 'complete', res)
					typeof params.complete == 'function' && params.complete(res);
				}
			});
		}
	},
	printSuccessLog(url, method, data, params, title, res) {
		var urls = ''
		urls = params.api.url
		// var decryptdata = res;
		// if(params.api.isAes){
		// 	Object.keys(decryptdata.data).forEach((key) => {
		// 		if(key == 'data'){
		// 			var decrypt = JSON.parse(aes_utils.AesDecrypt(decryptdata.data[key]));
		// 			 decryptdata.data.data = decrypt;
		// 			 // console.log(decryptdata.data)
		// 		}
		// 	})
		// }

		var myObj1 = {
			'url:': url,
			'data_decrypt:': data,
			'res': res.data
		};
		// var myObj1 = {'url:':url,'data_decrypt:':data,'res':res};
		// var myObj1 = {'url:':url,'method:':method,'params:':params,'data_decrypt:':data,'res':res.data};
		// if( urls == '/api/order/orderList' ){
			// console.log("Success-------->>>\n: " + JSON.stringify(myObj1));
			// console.log(myObj1);
		// } 

	},
	printCompleteLog(url, method, data, params, title, res) {
		var urls = ''
		if (params.api != undefined) {
			urls = params.api.url
		} else {
			urls = params.url
		}
		var myObj1 = {
			'url:': url,
			'method:': method,
			'params:': params,
			'data_decrypt:': data,
			'res': res.data
		};
		// console.log("Complete-------->>>\n: " + JSON.stringify(myObj1));
	},
	printErrLog(url, method, data, params, title, res) {
		var urls = ''
		if (params.api != undefined) {
			urls = params.api.url
		} else {
			urls = params.url
		}
		var myObj1 = {
			'url:': url,
			'method:': method,
			'params:': params,
			'data_decrypt:': data,
			'res': res
		};
		console.log("Err-------->>>\n: " + JSON.stringify(myObj1));
	},

}
